package cn.hfjava.kz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hfjava.kz.entity.po.Permission;
import cn.hfjava.kz.service.PermissionService;
import cn.hfjava.kz.mapper.PermissionMapper;
import jakarta.annotation.Resource;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author lds
* @description 针对表【permissions(权限表)】的数据库操作Service实现
* @createDate 2024-07-23 09:51:13
*/
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
    implements PermissionService{

    @Resource
    private PermissionMapper permissionMapper;

    /**
     * 获取所有的权限
     */
    @Override
    public List<Permission> findAllPermission() {
        List<Permission> permList = permissionMapper.selectList(null);
        return permList;
    }

    /**
     * 构建权限树结构
     *
     * @param permList
     */
    @Override
    public List<Permission> buildPermissionTree(List<Permission> permList) {
       List<Permission> treeList = Lists.newArrayList();
        for (Permission curPerm : permList) {
            if(0 == curPerm.getPid()) {
                treeList.add(findChildren(curPerm, permList));
            }
        }
       return treeList;
    }

    /**
     * 查询当前节点下所有的子节点集合
     * @param curPerm 当前节点对象
     * @param permList 所有菜单权限集合
     * @return
     */
    private Permission findChildren(Permission curPerm, List<Permission> permList) {
        curPerm.setChildren(Lists.newArrayList()); // []
        for (Permission childPerm : permList) {
            if(childPerm.getPid() == curPerm.getId()) {
                if (curPerm.getChildren() == null) {
                    curPerm.setChildren(Lists.newArrayList());
                }
                // 递归算法进行深度遍历
                curPerm.getChildren().add(findChildren(childPerm, permList));
            } // ## end if
        } // # end for
        return curPerm;
    }

    /**
     * 获取权限树
     *
     * @return
     */
    @Override
    public List<Permission> listWithTree() {
        //QueryWrapper用于构建查询条件
        // 查询所有的菜单权限列表
        List<Permission> permList  = permissionMapper.selectList(null);

        // 查询一级菜单权限信息列表，即pid=0的菜单权限
        List<Permission> rootList = permList.stream()
                .filter(perm -> perm.getPid() == 0)
                .collect(Collectors.toList());
        //组装菜单权限树
        rootList.forEach(root -> {
            // 查询根节点下所有的子节点集合
            List<Permission> childList = getChild(root, permList);
            root.setChildren(childList);
        });
        return rootList;
    }

    /**
     * 使用递归算法把无限级菜单权限组织为树形数据结构
     * @param root
     * @param list
     * @return
     */
    private List<Permission> getChild(Permission root,List<Permission> list){
        List<Permission> subList = list.stream()
                .filter(perm -> perm.getPid() == root.getId())
                .map(perm -> {

                    List<Permission> childList = getChild(perm, list);// 递归

                    perm.setChildren(childList);

                    return perm;
                }).collect(Collectors.toList());
        return subList;
    }
}




